<template>
	<view>
		<view class="block">
			<view class="title">
				我的账户
			</view>
			<view class="content">
				<view class="my">
					我的账户余额：{{balance}} 元
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				充值金额
			</view>
			<view class="content">
				<view class="amount">
					<view class="list">
						<view class="box" v-for="(amount,index) in amountList" :key="index" @tap="select(amount)"
							:class="{'on':amount == inputAmount}">
							{{amount}}元
						</view>
					</view>
					<view class="num">
						<view class="text">
							自定义充值金额
						</view>
						<view class="input">
							<input type="number" v-model="inputAmount" />
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				选择支付方式
			</view>
			<view class="content">
				<view class="pay-list">
					<!-- <view class="row" @tap="paytype='fast_pay'">
						<view class="left">
							<image src="/static/img/wxpay.png"></image>
						</view>
						<view class="center">
							快捷支付
						</view>
						<view class="right">
							<radio :checked="paytype=='fast_pay'" color="#f06c7a" />
						</view>
					</view>
					<view class="banklist" v-show="paytype == 'fast_pay'">
						<view class="list">
							<view :class="choose_bank == index?'row active':'row '" v-for="(row,index) in addressList"
								:key="index" @tap="selectbank(row,index)">
								<view class="left">
									<view class="head">
										{{row.bank_name|substr(0,2)}}
									</view>
								</view>
								<view class="center">
									<view class="name-tel">
										<view class="name">{{row.bank_name}}</view>
									</view>
									<view class="address">
										{{row.card_id}}
									</view>
								</view>
							</view>
						</view>
						<view class="add" @click="BankAdd">
							<uni-icons type="plus" size="30"></uni-icons><text>新增银行卡</text>
						</view>
					</view> -->
					<view class="row" @tap="paytype='bank'">
						<view class="left">
							<image src="/static/img/wxpay.png"></image>
						</view>
						<view class="center">
							银行卡支付
						</view>
						<view class="right">
							<radio :checked="paytype=='bank'" color="#f06c7a" />
						</view>
					</view>
					<view class="banklist add" v-if="paytype == 'bank'">
						<view class="bankitem ">
							<view class="left">
								银行卡名称 :
							</view>
							<view class="center">
								{{recharge_conf.bank_name}}
							</view>
						</view>
						<view class="bankitem">
							<view class="left">
								银行卡号 :
							</view>
							<view class="center">
								{{recharge_conf.bank_num}}
							</view>
						</view>
						<view class="bankitem">
							<view class="left">
								姓名 :
							</view>
							<view class="center">
								{{recharge_conf.bank_user}}
							</view>
						</view>
					</view>
					<view class="row" @tap="paytype='alipay'">
						<view class="left">
							<image src="/static/img/alipay.png"></image>
						</view>
						<view class="center">
							支付宝支付
						</view>
						<view class="right">
							<radio :checked="paytype=='alipay'" color="#f06c7a" />
						</view>
					</view>
					<view class="payimg" v-if="paytype == 'alipay'">
						<image :src="recharge_conf.ali_code | getImg" mode="widthFix"></image>
					</view>
					<view class="row" @tap="paytype='wxpay'">
						<view class="left">
							<image src="/static/img/wxpay.png"></image>
						</view>
						<view class="center">
							微信支付
						</view>
						<view class="right">
							<radio :checked="paytype=='wxpay'" color="#f06c7a" />
						</view>
					</view>
					<view class="payimg" v-if="paytype == 'wxpay'">
						<image :src="recharge_conf.wechat_code | getImg" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="upload">
			<view class="title">
				上传支付凭证
			</view>
			<view class="imgbox">
				<image v-if="voucher_url!=''" :src="voucher_url | getImg" @click="uploadImg" mode="widthFix"></image>
				<image v-else src="/static/img/add.png" @click="uploadImg" mode="widthFix"></image>
			</view>
		</view>
		<view class="pay">
			<view class="btn" @tap="doDeposit">立即充值</view>
			<!-- <view class="tis">
				点击立即充值，即代表您同意<view class="terms">
					《条款协议》
				</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		Index,
		Recharge
	} from "@/api/user.js"
	import {
		FastList
	} from "@/api/fast.js"
	export default {
		data() {
			return {
				available: true,
				balance: 0,
				inputAmount: '', //金额
				amountList: [10, 50, 100], //预设3个可选快捷金额
				paytype: 'bank', //支付类型
				addressList: [],
				choose_bank: -1,
				token_no: '',
				recharge_conf: [],
				voucher_url: ''
			};
		},
		filters: {
			substr(value, start, length) {
				console.log(value);
				if (value == '') return '';
				return value.substring(start, length)
			},
		},
		methods: {
			uploadImg() {
				var that = this
				uni.chooseImage({
					count: 1,
					success(res) {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						for (let i = 0; i < igmFile.length; i++) {
							uni.uploadFile({
								url: getApp().globalData.data.requestUrl +
									'/upload/recharge',
								header: {
									'token': uni.getStorageSync('token')
								},
								filePath: igmFile[i],
								name: 'image',
								success: (res) => {
									uni.hideLoading()
									let imgRes = JSON.parse(res.data); //微信和头条支持
									if (imgRes.status == 1) {
										let imgData = imgRes.data
										that.voucher_url = imgData.path
									} else {
										uni.showToast({
											title: res.message,
											icon: "none"
										})
										return
									}
								},
								fail(err) {
									uni.hideLoading()
								}
							})
						}
					}
				})
			},
			BankList() {
				FastList().then(res => {
					console.log(res);
					this.addressList = res.data
				})
			},
			selectbank(row, index) {
				console.log(index);
				this.choose_bank = index
				this.token_no = this.addressList[this.choose_bank].token_no
			},
			BankAdd() {
				uni.navigateTo({
					url: '/pages/user/bindcard/bindbank/index'
				})
			},
			select(amount) {
				this.inputAmount = amount;
			},
			getData() {
				Index().then(res => {
					this.balance = res.data.info.balance_1
					this.recharge_conf = res.data.recharge_conf
				})
			},
			doDeposit() {
				if (!this.available) {
					return false
				}
				if (parseFloat(this.inputAmount).toString() == "NaN") {
					uni.showToast({
						title: '请输入正确金额',
						icon: 'none'
					});
					return;
				}
				if (this.inputAmount <= 0) {
					uni.showToast({
						title: '请输入大于0的金额',
						icon: 'none'
					});
					return;
				}
				if (parseFloat(this.inputAmount).toFixed(2) != parseFloat(this.inputAmount)) {
					uni.showToast({
						title: '最多只能输入两位小数哦~',
						icon: 'none'
					});
					return;
				}
				//模板模拟支付，实际应用请调起微信/支付宝
				uni.showLoading({
					title: '支付中...'
				});
				this.available = false
				Recharge({
					pay_type: this.paytype,
					num: this.inputAmount,
					token_no: this.token_no,
					voucher_url: this.voucher_url
				}).then(res => {
					uni.hideLoading()
					uni.$showMsg(res.message)
					if (res.status == 1) {
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/user/record/record'
							})
						}, 500)
					}
					return
					if (res.status == 0) {

						this.available = true
						return false
					} else {
						if (this.paytype == 'alipay') {
							//支付宝app支付
							//支付宝app支付
							plus.runtime.openURL(res.data.expend.jump_id)
							return false
							uni.requestPayment({
								provider: "alipay",
								orderInfo: res.data,
								success: (data) => {
									this.available = false
									console.log(data);
									uni.showToast({
										title: '支付成功',
										icon: "none"
									})
									setTimeout(() => {
										uni.redirectTo({
											url: '../../pay/success/success?amount=' +
												this.inputAmount
										});
									}, 300)

								},
								fail: (err) => {
									console.log(err);
									this.available = true
								}
							})
						} else if (this.paytype == 'alipay_wap') {
							//支付宝h5
							window.location.href = res.data.expend.ali_h5_pay_url
							// uni.redirectTo({
							// 	url: '/pages/alipay/alipay?content=' + res.data
							// })
						} else if (this.paytype == 'wxpay_wap') {
							//微信h5
							window.location.href = res.data
						} else if (this.paytype == 'fast_pay') {
							//快捷支付
							uni.navigateTo({
								url: '/pages/pay/payment/sms/index?payment_id=' + res.data.id
							})
							return false
						}
					}
				})

			}
		},
		onLoad() {
			// // #ifdef APP-PLUS
			// this.paytype = 'alipay'
			// // #endif
			// // #ifdef MP-WEIXIN
			// this.paytype = 'mppay'
			// // #endif
			// // #ifdef H5
			// this.paytype = 'alipay_wap'
			// // #endif
			this.getData()
		},
		onShow() {
			this.BankList()
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 10px;
	}

	.banklist {
		.add {
			display: flex;
			align-items: center;
			font-size: 14px;
			padding: 10px 2%;
			border-bottom: 1px solid #ddd;

			text {
				margin-right: 10px;
			}
		}

		.bankitem {
			font-size: 14px;
			padding: 10px;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #eee;

			.left {
				margin-right: 10px;
				width: 100px;
			}
		}
	}

	.payimg {
		text-align: center;

		image {
			width: 70%;
		}
	}

	.upload {
		padding: 0 2%;
		margin: 20px 0;

		image {
			width: 80px;
			margin-top: 20px;
			margin-right: 20px;
		}
	}

	.list {
		flex-wrap: wrap;

		.row {
			width: 96vw;
			padding: 20upx 2%;

			.left {
				width: 90upx;
				flex-shrink: 0;
				align-items: center;

				.head {
					display: flex;
					width: 70upx;
					height: 70upx;
					background: linear-gradient(to right, #ccc, #aaa);
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 25upx;
				}
			}

			.center {
				width: 100%;
				flex-wrap: wrap;

				.name-tel {
					width: 100%;
					align-items: baseline;

					.name {
						font-size: 34upx;
					}

					.tel {
						margin-left: 30upx;
						font-size: 24upx;
						color: #777;
					}

					.default {

						font-size: 22upx;

						background-color: #f06c7a;
						color: #fff;
						padding: 0 18upx;
						border-radius: 24upx;
						margin-left: 20upx;
					}
				}

				.address {
					width: 100%;
					font-size: 24upx;
					align-items: baseline;
					color: #777;
				}
			}

			.right {
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;

				.icon {
					justify-content: center;
					align-items: center;
					width: 80upx;
					height: 60upx;
					border-left: solid 1upx #aaa;
					font-size: 40upx;
					color: #777;
				}
			}
		}

		.active {
			background: #d1d1d1;
			border-radius: 4px;
		}
	}

	.block {
		width: 94%;
		padding: 20upx 3%;

		.title {
			width: 100%;
			font-size: 34upx;
		}

		.content {
			.my {
				width: 100%;
				height: 120upx;
				display: flex;
				align-items: center;
				font-size: 30upx;
				border-bottom: solid 1upx #eee;
			}

			.amount {
				width: 100%;

				.list {
					display: flex;
					justify-content: space-between;
					padding: 20upx 0;

					.box {
						width: 30%;
						height: 120upx;
						display: flex;
						justify-content: center;
						align-items: center;
						border-radius: 10upx;
						box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.05);
						font-size: 36upx;
						background-color: #f1f1f1;
						color: 333;

						&.on {
							background-color: #f06c7a;
							color: #fff;
						}
					}
				}

				.num {
					margin-top: 10upx;
					display: flex;
					justify-content: flex-end;
					align-items: center;

					.text {
						padding-right: 10upx;
						font-size: 30upx;
					}

					.input {
						width: 28.2vw;
						border-bottom: solid 2upx #999;

						justify-content: flex-end;
						align-items: center;

						input {
							margin: 0 20upx;
							height: 60upx;
							font-size: 30upx;
							color: #f06c7a;
							justify-content: flex-end;
							align-items: center;
						}
					}
				}
			}

			.pay-list {
				width: 100%;
				border-bottom: solid 1upx #eee;

				.row {
					width: 100%;
					height: 120upx;
					display: flex;
					align-items: center;

					.left {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						align-items: center;

						image {
							width: 80upx;
							height: 80upx;
						}
					}

					.center {
						width: 100%;
						font-size: 30upx;
					}

					.right {
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						justify-content: flex-end;
					}
				}
			}
		}
	}

	.pay {
		margin-top: 20upx;
		width: 100%;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;

		.btn {
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			background-color: #f06c7a;
			box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.2);
		}

		.tis {
			margin-top: 10upx;
			width: 100%;
			font-size: 24upx;
			display: flex;
			justify-content: center;
			align-items: baseline;
			color: #999;

			.terms {
				color: #5a9ef7;
			}
		}
	}
</style>